package com.xy.iotest;

import java.io.*;

/**
 * @author 谢阳
 * @version 1.8.0_131
 * @date 2022/6/20 13:35
 * @description
 */
public class LowerCaseInputStream extends FilterInputStream {
    /**
     * Creates a <code>FilterInputStream</code>
     * by assigning the  argument <code>in</code>
     * to the field <code>this.in</code> so as
     * to remember it for later use.
     *
     * @param in the underlying input stream, or <code>null</code> if
     *           this instance is to be created without an underlying stream.
     */
    protected LowerCaseInputStream(InputStream in) {
        super(in);
    }

    @Override
    public int read() throws IOException {
        int c = in.read();
        return (c == -1 ? c : Character.toLowerCase(c));
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        int result = in.read(b, off, len);
        for (int i = off; i< off + result; i++) {
            b[i] = (byte) Character.toLowerCase((char) b[i]);
        }
        return result;
    }

    public static void main(String[] args) throws Exception {
        int c;

            InputStream inputStream = new LowerCaseInputStream(new BufferedInputStream(new FileInputStream("decorate/src/com/xy/iotest/test.txt")));
            while ((c =inputStream.read()) >= 0) {
                System.out.print((char) c);
            }
            inputStream.close();
    }
}
